#include "kernel_operator.h"

using namespace AscendC;

// constexpr int32_t TOTAL_LENGTH = 8 * 2048;
// constexpr int32_t USE_CORE_NUM = 8;                                    // num of core used
// constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;          // length computed of each core
constexpr int32_t BLOCK_LENGTH = 32;
constexpr int32_t TILE_NUM = 8;                                        // split data into 8 tile for each core
constexpr int32_t BUFFER_NUM = 2;                                      // tensor num for each queue
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;  // seperate to 2 parts, due to double buffer

class CurrOp {
public:
    __aicore__ inline CurrOp() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y)
    {
        xGm.SetGlobalBuffer((__gm__ half *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        yGm.SetGlobalBuffer((__gm__ half *)y + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(tempQueue_1, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(tempQueue_2, TILE_LENGTH * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        // 将Global Memory上的输入Tensor搬运到Local Memory
        LocalTensor<half> xLocal = inQueueX.AllocTensor<half>();
        DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        pipe_barrier(PIPE_ALL);
        inQueueX.EnQue(xLocal);

        // 打印调试信息
        // uint32_t array[] = {static_cast<uint32_t>(1024), static_cast<uint32_t>(2048)};
        // AscendC::ShapeInfo shapeInfo(2, array);
        // AscendC::DumpTensor(xLocal, 1, 64, shapeInfo);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        // 对Local Memory上的Tesnor进行运算，结果存储到输出Tensor
        LocalTensor<half> xLocal = inQueueX.DeQue<half>();
        LocalTensor<half> tempTensor = tempQueue_1.Get<half>();
        LocalTensor<half> yLocal = outQueueY.AllocTensor<half>();

        Exp(yLocal, xLocal, TILE_LENGTH); // y=exp(x)
        pipe_barrier(PIPE_ALL);

        Muls(tempTensor, xLocal, half(-1.0), TILE_LENGTH);  // tmp=-x
        pipe_barrier(PIPE_ALL);

        Exp(tempTensor, tempTensor, TILE_LENGTH);  // tmp=exp(-x)
        pipe_barrier(PIPE_ALL);

        Sub(yLocal, yLocal, tempTensor, TILE_LENGTH);  // y = exp(x) - exp(-x)
        pipe_barrier(PIPE_ALL);

        Muls(yLocal, yLocal, half(0.5), TILE_LENGTH);  // y = (exp(x) - exp(-x)) / 2
        pipe_barrier(PIPE_ALL);

        outQueueY.EnQue<half>(yLocal);
        inQueueX.FreeTensor(xLocal);

        // 打印调试信息
        // uint32_t array[] = {static_cast<uint32_t>(1024), static_cast<uint32_t>(2048)};
        // AscendC::ShapeInfo shapeInfo(2, array);
        // AscendC::DumpTensor(yLocal, 1, 64, shapeInfo);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        // 将输出Tensor从Local Memory搬运至Global Memory
        LocalTensor<half> yLocal = outQueueY.DeQue<half>();
        DataCopy(yGm[progress * TILE_LENGTH], yLocal, TILE_LENGTH);
        // pipe_barrier(PIPE_ALL);
        // 打印调试信息
        // uint32_t array[] = {static_cast<uint32_t>(1024), static_cast<uint32_t>(2048)};
        // AscendC::ShapeInfo shapeInfo(2, array);
        // AscendC::DumpTensor(yGm, 3, 64, shapeInfo);
        // AscendC::DumpTensor(yLocal, 4, 64, shapeInfo);
        outQueueY.FreeTensor(yLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tempQueue_1;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tempQueue_2;
    AscendC::GlobalTensor<half> xGm;
    AscendC::GlobalTensor<half> yGm;
};

extern "C" __global__ __aicore__ void curr_op(GM_ADDR x, GM_ADDR y)
{
    CurrOp op;
    op.Init(x, y);
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void curr_op_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *y)
{
    curr_op<<<blockDim, nullptr, stream>>>(x, y);
}
#endif
